package com.atguigu.eduservice.controller;


import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import common.atguigu.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    //注入VodClient远程调用
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        boolean save = videoService.save(eduVideo);
        return save ? R.ok().message("添加课程小节成功") : R.error().message("添加课程小节失败");
    }

    //删除小节,删除阿里云视频
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable String id) {
        //根据小节id得到视频id，用于删除视频
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();//视频id
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)) {
            //有视频id，远程调用方法进行删除视频
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节
    //根据id查询小节  回显
    @GetMapping("/getVideo/{id}")
    public R getVideo(@PathVariable String id) {
        EduVideo eduVideo = videoService.getById(id);
        return R.ok().data("eduVideo", eduVideo);
    }

    //修改
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        boolean updateById = videoService.updateById(eduVideo);
        return updateById ? R.ok().message("修改课程小节成功") : R.error().message("修改课程小节失败");
    }
}

